package controller.member;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import model.mail.MailHelper;

public class SendConfirmMailServlet extends HttpServlet {
	private static final long serialVersionUID = 5908866614720969739L;

	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		HttpSession session = req.getSession();
		
		if (session.getAttribute("id") == null || session.getAttribute("confirmMailAddr") == null ||
				session.getAttribute("nickname") == null)
			resp.sendRedirect("./index");
		else {
			req.setAttribute("id", session.getAttribute("id"));
			req.setAttribute("confirmMailAddr", session.getAttribute("confirmMailAddr"));
			req.setAttribute("nickname", session.getAttribute("nickname"));
			req.getRequestDispatcher("./member/sendConfirmMail.jsp").forward(req, resp);
		}
	}
	
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		String confirmMailAddr = (String) req.getParameter("email");
		String nickname = (String) req.getParameter("nickname");
		String id = (String) req.getParameter("id");
		String subject = nickname + "先生/小姐您好，再次感謝您註冊成為課程評論社群的會員"
	    		+ "，此為電子信箱認證信，點選認證連結後即可啟用帳號";
		String msgBody = nickname + "先生/小姐您好:\n\n再次感謝您註冊成為此社群的會員\n\n以下為認證連結:\n\n"
    			+ "http://coucomm.appspot.com/confirmMember?key=" + id
    			+ "\n\n請點選認證連結即可完成認證並且立刻開始使用此社群!\n\n課程評論社群管理員";
		
		MailHelper.send("a0838809919@gmail.com", "課程評論社群管理員", confirmMailAddr, nickname + "先生/小姐", subject, msgBody);
	}
}
